//
//  PersonalPageTableViewCell.m
//  budejiePersonalPage
//
//  Created by 李叶夫 on 16/8/25.
//  Copyright © 2016年 李叶夫. All rights reserved.
//

#import "PersonalPageTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "ContentModel.h"
#import <UIImage+GIF.h>

@interface PersonalPageTableViewCell ()

@property (weak, nonatomic) IBOutlet UIImageView *userIcon;

@property (weak, nonatomic) IBOutlet UILabel *userName;

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UILabel *tagLabel;

@property (weak, nonatomic) IBOutlet UIImageView *tagIcon;

@property (weak, nonatomic) IBOutlet UIButton *dingButton;

@property (weak, nonatomic) IBOutlet UIButton *caiButton;

@property (weak, nonatomic) IBOutlet UIButton *shareButton;

@property (weak, nonatomic) IBOutlet UIButton *commentButton;

@property (weak, nonatomic) IBOutlet UIImageView *AVImageView;
@property (weak, nonatomic) IBOutlet UIView *commentView;

@property (weak, nonatomic) IBOutlet UILabel *commentTagLabel;
@property (weak, nonatomic) IBOutlet UILabel *commentNameLabel;

@property (weak, nonatomic) IBOutlet UILabel *commentTextLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *commentHeightConstraint;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heighttConstraint;

@end

@implementation PersonalPageTableViewCell

- (void)setModel:(ContentModel *)model
{
    _model = model;
    
    
    
    if (model.topic) {//为评论页的cell
        NSDictionary *topic = model.topic;
        NSDictionary *user = topic[@"u"];
        
        [self.userIcon sd_setImageWithURL:[NSURL URLWithString:user[@"header"][0]]];
        self.userName.text = user[@"name"];
        self.timeLabel.text = topic[@"passtime"];
        self.titleLabel.text = topic[@"text"];
        
        //判断cell的类型
        NSDictionary *image = topic[@"image"];
        NSDictionary *gif = topic[@"gif"];
        if ([topic[@"type"] isEqualToString:@"image"]) {//图片
            self.AVImageView.hidden = NO;
            
            NSNumber *height = image[@"height"];
            NSNumber *width = image[@"width"];
            CGFloat caculateHeight = height.floatValue / width.floatValue * self.AVImageView.bounds.size.width;
            
            //大图判断
            self.heighttConstraint.constant = (caculateHeight > 1000) ? 400 : caculateHeight;

            [self.AVImageView sd_setImageWithURL:[NSURL URLWithString:image[@"big"][0]]];
        }else if ([topic[@"type"] isEqualToString:@"text"]){//存文本
            
            self.AVImageView.hidden = YES;
            self.AVImageView.image = nil;
            self.heighttConstraint.constant = 0;
            
        }else if ([topic[@"type"] isEqualToString:@"gif"]){//gif
            
            self.AVImageView.hidden = NO;
            
            NSNumber *height = gif[@"height"];
            NSNumber *width = gif[@"width"];
            
            self.heighttConstraint.constant = height.floatValue / width.floatValue * self.AVImageView.bounds.size.width;
            
            [self.AVImageView sd_setImageWithURL:[NSURL URLWithString:gif[@"images"][0]]];
            
        }else{//视频
            
            self.AVImageView.hidden = YES;
            self.AVImageView.image = nil;
            self.heighttConstraint.constant = 0;
            
        }
        
        //标签
        NSArray *tagsArr = topic[@"tags"];
        
        NSMutableString *tempM = [NSMutableString string];
        
        for (NSDictionary *dic in tagsArr) {
            
            [tempM appendFormat:@" %@ ",dic[@"name"]];
        }
        
        self.tagLabel.text = tempM.copy;
        
        
        [self.dingButton setTitle:topic[@"up"] forState:UIControlStateNormal];
        [self.caiButton setTitle:[NSString stringWithFormat:@"%@",topic[@"down"]] forState:UIControlStateNormal];
        [self.commentButton setTitle:topic[@"comment"] forState:UIControlStateNormal];
        
        self.commentView.hidden = NO;
        self.commentHeightConstraint.constant = 44;
        self.commentNameLabel.text = [NSString stringWithFormat:@"%@:",model.u[@"name"]];
        self.commentTextLabel.text = model.content;

    }else{//为帖子和分享叶的cell
        
        NSDictionary *u = model.u;
        
        NSArray *headerArr = u[@"header"];
        
        NSArray *tagsArr = model.tags;
        
        [self.userIcon sd_setImageWithURL:[NSURL URLWithString:headerArr[0]]];
        
        self.userName.text = u[@"name"];
        
        self.timeLabel.text = model.passtime;
        
        self.titleLabel.text = model.text;
        
        NSMutableString *tempM = [NSMutableString string];
        
        for (NSDictionary *dic in tagsArr) {
            
            [tempM appendFormat:@" %@ ",dic[@"name"]];
        }
        
        self.tagLabel.text = tempM.copy;
        
        [self.dingButton setTitle:model.up forState:UIControlStateNormal];
        [self.caiButton setTitle:[NSString stringWithFormat:@"%@",model.down] forState:UIControlStateNormal];
        [self.shareButton setTitle:[NSString stringWithFormat:@"%@",model.forward] forState:UIControlStateNormal];
        
        if (model.top_comment) {
            self.commentView.hidden = NO;
            self.commentHeightConstraint.constant = 44;
            self.commentNameLabel.text = [NSString stringWithFormat:@"%@:",model.top_comment[@"u"][@"name"]];
            self.commentTextLabel.text = model.top_comment[@"content"];
            //评论的uid与发布者的uid进行比较
            if ([u[@"uid"] isEqualToString:model.top_comment[@"u"][@"uid"]]) {
                self.commentTagLabel.text = @"评\n论";
            }else{
                self.commentTagLabel.text = @"热\n评";
            }
        }else{
            
            self.commentView.hidden = YES;
            self.commentHeightConstraint.constant = 0;
            
        }
        
        
        
        
        
        
        if ([model.type isEqualToString:@"image"]) {//图片
            self.AVImageView.hidden = NO;
            //获取图片的宽高
            NSNumber *width = model.image[@"width"];
            NSNumber *height = model.image[@"height"];
            
            CGFloat caculateHeight = height.floatValue / width.floatValue * self.AVImageView.bounds.size.width;
            //大图判断
            self.heighttConstraint.constant = (caculateHeight > 1000) ? 400 : caculateHeight;
//            self.heighttConstraint.constant = 200;
            [self.AVImageView sd_setImageWithURL:[NSURL URLWithString:model.image[@"big"][0]]];
            
        }else if ([model.type isEqualToString:@"text"]) {//纯文本
            
            self.AVImageView.hidden = YES;
            self.heighttConstraint.constant = 0;
            self.AVImageView.image = nil;
            
        }else if ([model.type isEqualToString:@"gif"]) {//gif
            self.AVImageView.hidden = NO;
            //取出gif图片的宽高
            NSNumber *width = model.gif[@"width"];
            NSNumber *height = model.gif[@"height"];
            
            self.heighttConstraint.constant = height.floatValue / width.floatValue * self.AVImageView.bounds.size.width;
//            self.heighttConstraint.constant = 200;
            NSURL *url = [NSURL URLWithString:model.gif[@"images"][0]];
            [self.AVImageView sd_setImageWithURL:url];
            
            
        }else if ([model.type isEqualToString:@"video"]){//视频
            self.AVImageView.hidden = YES;
            self.AVImageView.image = nil;
            self.heighttConstraint.constant = 0;
            
        }
        
        
    }
    
    
}



- (void)layoutSubviews
{
    [super layoutSubviews];
    


}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.userIcon.layer.cornerRadius = self.userIcon.bounds.size.width / 2;
    
    self.userIcon.clipsToBounds = YES;
    self.userIcon.layer.masksToBounds = YES;
    
    
    
    self.AVImageView.layer.masksToBounds = YES;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end














